package com.hejian.android.a72ggametools.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.GiftInfo;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.viewholder.HandGiftViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 手游礼包ListView适配器
 * Created by Administrator on 2016/1/11.
 */
public class HandGiftAdapter extends BaseAdapter {
    List<GiftInfo> giftInfoList;
    private Context context;
    private HandGiftViewHolder handGiftViewHolder;
    private ImageLoader imageLoader;
    private ImageCache imageCache = new DoubleCache();

    public HandGiftAdapter(List<GiftInfo> giftInfoList, Context context) {
        this.giftInfoList = giftInfoList;
        this.context = context;
        imageLoader = new ImageLoader(context);
        imageLoader.setImageCache(imageCache);
    }

    @Override
    public int getCount() {
        return giftInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return giftInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.handgame_listview_item, null);
            handGiftViewHolder = new HandGiftViewHolder();
            handGiftViewHolder.imageView = (ImageView) view.findViewById(R.id.game_gift_img);
            handGiftViewHolder.titleTextView = (TextView) view.findViewById(R.id.game_gift_title);
            handGiftViewHolder.remainTextView = (TextView) view.findViewById(R.id.game_gift_count);
            handGiftViewHolder.contentTextView = (TextView) view.findViewById(R.id.game_gift_description);
            handGiftViewHolder.button = (Button) view.findViewById(R.id.game_gift_receive);
            view.setTag(handGiftViewHolder);
        } else {
            handGiftViewHolder = (HandGiftViewHolder) view.getTag();
        }
        if (giftInfoList != null && giftInfoList.size() > 0) {
            GiftInfo giftInfo = giftInfoList.get(position);
            handGiftViewHolder.titleTextView.setText(giftInfo.getName());
            handGiftViewHolder.remainTextView.setText(giftInfo.getRemain());
            handGiftViewHolder.contentTextView.setText(giftInfo.getContent());
            if (giftInfo.getRemain().equals("0")) {
                handGiftViewHolder.button.setBackgroundResource(R.drawable.shap_white);
                handGiftViewHolder.button.setText("淘号");
                int orangeColor = Color.argb(255,239,128,0);
                handGiftViewHolder.button.setTextColor(orangeColor);
            } else {
                handGiftViewHolder.button.setBackgroundResource(R.drawable.shap_blue);
                handGiftViewHolder.button.setText("免费领取");
                handGiftViewHolder.button.setTextColor(Color.BLUE);
            }
            handGiftViewHolder.imageView.setImageBitmap(null);
            imageLoader.displayImage(giftInfo.getIcon(), handGiftViewHolder.imageView);

        }
        return view;
    }


    public void freshListView(List<GiftInfo> giftInfoList){
        this.giftInfoList = giftInfoList;
        notifyDataSetChanged();
    }
}
